import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

// 放在这里的是默认创建时就带上的路由
// 所有人都能看，也叫静态路由
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true // 当路由中的属性**`hidden`**为true时，表示该路由不显示在左侧菜单中
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true // 当路由中的属性**`hidden`**为true时，表示该路由不显示在左侧菜单中
  },
  {
    path: '/import',
    component: Layout,
    children: [{
      path: '',
      component: () => import('@/views/import')
    }]
  },

  {
    path: '/',
    name: 'dashboard',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: {
        title: '首页',
        icon: 'dashboard'
      }
    }]
  }

  // 404 page must be placed at the end !!!
//   {
//     path: '*',
//     redirect: '/404',
//     hidden: true
//   }
]

// 引入多个模块的规则
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'

// 动态路由
// 这里另外管理的路由数组，应该是权限校验之后再追加
// 只有有权限的人才能看，也叫 动态路由
export const asyncRoutes = [
  approvalsRouter,
  departmentsRouter,
  employeesRouter,
  permissionRouter,
  attendancesRouter,
  salarysRouter,
  settingRouter,
  socialRouter
]
const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }), // 管理滚动行为 如果出现滚动 切换就让 让页面回到顶部
  routes: constantRoutes // 临时合并所有的路由
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
